package com.example.lib5.linkedList.test;


public class SingleLinkedListDemo {
    public static void main(String[] args) {
        //单向链表
        HeroList heroList = new HeroList();
        //插入单向链表
        Hero heroTangSang = new Hero(1, "唐三", "昊天宗");
        Hero heroXiaoWu = new Hero(2, "小舞", "十万年魂兽");
        Hero heroWuLieNa = new Hero(4, "胡列娜", "武魂殿");
        Hero heroBbd = new Hero(4, "比比东", "武魂殿");
        Hero heroQianRenXve = new Hero(3, "千仞雪", "武魂殿");
        heroList.addOrdered(heroTangSang);
        heroList.addOrdered(heroXiaoWu);
        heroList.addOrdered(heroWuLieNa);
        heroList.addOrdered(heroWuLieNa);
        heroList.addOrdered(heroWuLieNa);
        heroList.addOrdered(heroWuLieNa);
        heroList.addOrdered(heroQianRenXve);
        heroList.delete(5);
//        heroList.update(heroBbd);
        // TODO: 2022/4/24 0024 有序插入单向链表
        //遍历链表
        heroList.list();

    }

}

/**
 * 创建一个单向链表节点
 * name,no,background
 */
class Hero {
    Hero next;
    int no;
    String name;
    String bg;

    public Hero(int no, String name, String bg) {
        this.no = no;
        this.name = name;
        this.bg = bg;
    }

    @Override
    public String toString() {
        return "Hero{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", bg='" + bg + '\'' +
                '}';
    }
}

//链表，用于添加节点
class HeroList {
    //头结点
    Hero head = new Hero(0, "", "");

    public void add(Hero hero) {
        //遍历找到节点的最后一个temp，然后添加进去
        Hero temp = head;
        while (true) {
            //退出的情况，temp.next=null
            if (temp.next == null) {
                break;
            }
            temp = temp.next;
        }
        temp.next = hero;
    }

    public void list() {
        Hero temp = head.next;
        if (temp == null) {
            System.out.println("链表为空");
            return;
        }
        while (true) {
            if (temp == null) {
                break;
            }
            System.out.println(temp.toString());
            temp = temp.next;
        }
    }

    /**
     * 有序添加
     *
     * @param hero
     */
    public void addOrdered(Hero hero) {
        //判断节点是否为空，如果为空的话，就直接加入即可
        Hero temp = head;
        boolean flag = false;
        while (true) {
            if (temp.next == null) {
                break;
            }
            if (temp.next.no == hero.no) {
                flag=true;
                break;
            } else if (temp.next.no > hero.no) {
                break;
            }
            temp = temp.next;
        }
        if (flag) {
            System.out.println("这个节点已经有了,或不存在");
        }else {
            hero.next=temp.next;
            temp.next=hero;
        }
    }

    public void update(Hero hero) {
        Hero temp=head;
        boolean flag=false;
        while (true) {
            if (temp.next==null){
                flag=true;
                break;
            }
            if (temp.next.no==hero.no){
                break;
            }
            temp=temp.next;
        }

        if (flag){
            System.out.println("不存在这个节点，所以无法修改");
        }else {
            temp.next.name=hero.name;
            temp.next.bg=hero.bg;
        }
    }

    public void delete(int no) {
        Hero temp=head;
        //遍历链表
        while (true) {
            if (temp.next==null){
                System.out.println("表示没有找到这个节点");
                break;
            }
            if (temp.next.no==no){//表示找到这个节点了
                temp.next=temp.next.next;
                break;
            }
            temp=temp.next;
        }
    }
}

